import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:x3_component_core/components/state_components/base_empty_component.dart';
import 'package:x3_component_core/components/layout_components/base_screen_container_component.dart';

/// 所有页面的根容器
class BaseScaffoldComponent extends StatelessWidget {

  /// 标题
  final String title;

  /// 标题容器
  final Widget titleWidget;

  /// 子组件
  final Widget child;

  /// appbar 右侧按钮
  final List<Widget> actions;

  /// 背景颜色
  final Color backGroundColor;

  /// 打开底部按钮集
  final Widget footerWidget;

  /// 浮动按钮
  final Widget floatingActionButton;
  final FloatingActionButtonLocation floatingActionButtonLocation;
  final FloatingActionButtonAnimator floatingActionButtonAnimator;

  /// 返回事件的监听
  final Future<bool> Function() willPopScope;

  final Color appBarBackGroundColor;

  /// 右边按钮
  final Widget leading;

  /// 右边按钮宽度
  final double leadingWidth;

  /// 底部菜单
  final BottomNavigationBar bottomNavigationBar;

  /// 是否显示右边侧弹出框
  final bool showEndDrawer;

  /// 底部高度
  final double footerWidgetHeight;

  /// 键盘顶起
  final bool resizeToAvoidBottomInset;

  BaseScaffoldComponent({
    Key key,
    this.title,
    this.titleWidget,
    @required this.child,
    this.actions,
    this.backGroundColor,
    this.footerWidget,
    this.willPopScope,
    this.appBarBackGroundColor,
    this.floatingActionButton,
    this.floatingActionButtonLocation,
    this.floatingActionButtonAnimator,
    this.bottomNavigationBar,
    this.leading,
    this.leadingWidth,
    this.showEndDrawer = false,
    this.footerWidgetHeight = 56,
    this.resizeToAvoidBottomInset = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => BaseScaffoldDrawer(),
      child: BaseScreenContainerComponent(
        child: WillPopScope(
          onWillPop: _requestPop,
          child: Scaffold(
            backgroundColor: this.backGroundColor ?? Theme.of(context).scaffoldBackgroundColor,
            appBar: _buildAppBarWidget(context),
            body: SafeArea(child: this.child),
            endDrawer: this.showEndDrawer == true ? _buildEndDrawer() : null,
            endDrawerEnableOpenDragGesture: false,
            floatingActionButton: this.floatingActionButton,
            floatingActionButtonLocation: this.floatingActionButtonLocation,
            bottomNavigationBar: (this.footerWidget != null && bottomNavigationBar == null) ? this._buildScaffoldFooterWidget(context) : this.bottomNavigationBar,
            resizeToAvoidBottomInset: this.resizeToAvoidBottomInset,
          ),
        ),
      ),
    );
  }

  Widget _buildScaffoldFooterWidget(BuildContext context) {
    return SafeArea(
      child: Container(
        height: this.footerWidgetHeight,
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        color: Colors.white,
        width: MediaQuery.of(context).size.width,
        child: this.footerWidget,
      ),
    );
  }

  AppBar _buildAppBarWidget(BuildContext context) {
    if (this.title == null && this.titleWidget == null) return null;
    Widget leadingWidget = this.leading != null ? this.leading : this.showEndDrawer == true ? BackButton() : null;
    return AppBar(
      leading: leadingWidget,
      leadingWidth: leadingWidth,
      backgroundColor: appBarBackGroundColor,
      title: this.title == null ? this.titleWidget : Text(this.title, style: Theme.of(context).appBarTheme.textTheme?.headline1),
      centerTitle: true,
      actions: [Row(children: this.actions ?? [])],
      elevation: 2,
      shadowColor: Colors.black.withOpacity(0.1),
    );
  }

  // 监听返回
  Future<bool> _requestPop() {
    if (this.willPopScope == null) return Future.value(true);
    return this.willPopScope();
  }

  Widget _buildEndDrawer() {
    return Builder(
      builder: (context) {
        BaseScaffoldDrawer _model = Provider.of<BaseScaffoldDrawer>(context);
        return Drawer(child: SafeArea(child: _model.drawer ?? BaseEmptyComponent()));
      },
    );
  }
}

class BaseScaffoldDrawer with ChangeNotifier {

  Widget _drawer;

  Widget get drawer => _drawer;

  dynamic drawerData;

  set drawer(Widget child) {
    _drawer = child;
    notifyListeners();
  }

  void openDrawer(BuildContext context) {
    Scaffold.of(context).openEndDrawer();
  }

}
